package com.acme.bankapp.domain.bank;

public class CheckingAccount extends AbstractAccount {
	private final String OVERDRAFT_MUST_BE_POSITIVE = "Overdraft must be positive";

	private double overdraft;

	public CheckingAccount(final double amount, final double overdraft)
			throws NegativeArgumentValueException {
		super(amount);
		if (overdraft < 0) {
			throw new NegativeArgumentValueException(OVERDRAFT_MUST_BE_POSITIVE);
		}
		this.overdraft = overdraft;
	}

	/**
	 * if balance is not enough for withdraw, use overdraft's resources
	 * 
	 * @throws OverDraftLimitExceededException
	 * */
	@Override
	public void withdraw(final double amount)
			throws OverDraftLimitExceededException {

		if (amount > maximumAmountTowothdraw()) {
			throw new OverDraftLimitExceededException(this.toString() + "; Amount: " + amount);
		}

		setBalance(balance - amount);

	}

	@Override
	public double maximumAmountTowothdraw() {
		return getBalance() + getOverdraft();
	}

	public double getOverdraft() {
		return overdraft;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		long temp;
		temp = Double.doubleToLongBits(overdraft);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		CheckingAccount other = (CheckingAccount) obj;
		if (Double.doubleToLongBits(overdraft) != Double
				.doubleToLongBits(other.overdraft))
			return false;
		return true;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append(super.toString()).append("; Overdraft: ").append(overdraft);
		return sb.toString();
	}

}
